var vm = new Vue({ 
    el: '#container' 
    , data: {
        buttons: {}
        , query: {
            page: 1
            , pageSize: hcCore.pageSize
            , paramName: ''
            , paramDesc: ''
        }
        , opObj: {
            paramNo: ''
        }
        , tableData: {
            records: []
            , total: 0
        }
        , subParam: {
            visible: false
            , direction: 'rtl'
            , records: []
            , title: ''
        }
        , edit: {
            formId: 'editFrame'
            , title: '操作'
            , visible: false
            , fields: [{
                label: '参数编号'
                , name: 'param_no'
                , require: true
                , type: 'hidden'
                , disabled: false
                , value: ''
                , rules: {
                    required: true, message: '不能为空', trigger: 'blur'
                }
            }, {
                label: '参数名称'
                , name: 'param_name'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入参数名称'
                , rules: {
                    required: true, message: '不能为空', trigger: 'blur'
                }
            }, {
                label: '参数描述'
                , name: 'param_desc'
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入参数描述'
            }]
            , op: function () {
            }
        }
        , editSub: {
            formId: 'editSubFrame'
            , title: '操作'
            , visible: false
            , fields: [{
                label: '子项编号'
                , name: 'sub_no'
                , require: true
                , type: 'hidden'
                , disabled: false
                , value: ''
                , rules: {
                    required: true, message: '不能为空', trigger: 'blur'
                }
            }, {
                label: '参数编号'
                , name: 'param_no'
                , require: true
                , type: 'hidden'
                , disabled: false
                , value: ''
                , rules: {
                    required: true, message: '不能为空', trigger: 'blur'
                }
            }, {
                label: '显示值'
                , name: 'sub_name'
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入显示值'
            }, {
                label: '保存值'
                , name: 'sub_value'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入保存值'
                , rules: {
                    required: true, message: '不能为空', trigger: 'blur'
                }
            }]
            , op: function () {
            }
        }
    }
    , mounted: async function () { 
        const me = this; 
        me.buttons = await hcCore.getPowerButton(); 
        if (me.buttons.hasQuery) { 
            me.onQuery(); 
        } 
    } 
    , methods: { 
        handleButton: function (act_no, row) { 
            switch (act_no) { 
                case 'Add': 
                    this.showEdit('add', '新增'); 
                    break; 
                case 'Edit': 
                    this.showEdit('edit', '编辑', row); 
                    break; 
                case 'Delete': 
                    this.onDelete(row); 
                    break; 
                default: 
                    break; 
            } 
        } 
        , handleButtonSub: function (act_no, row) {
            switch (act_no) {
                case 'Add':
                    this.showEditSub('add', '新增子项'); 
                    break;
                case 'Edit':
                    this.showEditSub('edit', '编辑子项', row); 
                    break;
                case 'Delete':
                    this.onDeleteSub(row); 
                    break;
            }
        }
        , onQuery: function () { 
            let me = this; 
            hcCore.post({ 
                url: '/SysParam/List' 
                , param: { 
                    page: me.query.page 
                    , page_size: me.query.pageSize 
                    , param_name: me.query.paramName 
                    , param_desc: me.query.paramDesc 
                } 
                , success: function (rsp) { 
                    me.tableData.records = rsp.data; 
                    me.tableData.total = rsp.total; 
                } 
            }) 
        } 
        , onDetails: function (row) {
            let me = this;
            if (row) {
                me.opObj.paramNo = row.param_no;
                hcCore.post({
                    url: '/SysParamSub/List'
                    , param: {
                        param_no: me.opObj.paramNo
                    }
                    , success: function (rsp) {
                        me.subParam.records = rsp.data;
                        me.subParam.visible = true;
                        me.subParam.title = '【' + row.param_name + '】详细列表';
                    }
                });
            }
            else {
                hcCore.showError('请选择要展开记录');
            }
        }
        , onPageChange: function (page) { 
            this.query.page = page; 
            this.onQuery(); 
        } 
        , showEdit: async function (type, title, row) { 
            let me = this; 
            //if (me.$refs.edit.$refs[me.edit.formId] !== undefined) { 
            //    me.$refs.edit.$refs[me.edit.formId].resetFields(); 
            //} 
            //me.edit.fields.forEach((item, index) => { 
            //    item.disabled = false; 
            //}); 
            me.resetParams(me.edit.fields);
            me.edit.title = title; 
            me.edit.op = type == 'add' ? me.onInsert : (type == 'edit' ? me.onUpdate : function () { }); 
            if (type == 'edit') { 
                if (row) { 
                    await hcCore.post({ 
                        url: '/SysParam/Get/' + row.param_no 
                        , success: function (rsp) { 
                            me.edit.fields.forEach((item, index) => { 
                                if (rsp.data[item.name]) { 
                                    item.value = rsp.data[item.name]; 
                                } 
 
                                if (item.name == 'param_no') { 
                                    item.disabled = true; 
                                } 
                            }); 
                        } 
                        , fault: '获得数据失败' 
                    }); 
                } 
                else { 
                    hcCore.showError('请选择要编辑的记录'); 
                    return; 
                } 
            } else { 
                
            } 
            me.edit.visible = true; 
        } 
        , onInsert: function () { 
            let me = this; 
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => { 
                if (valid) { 
                    hcCore.post({ 
                        url: '/SysParam/Insert' 
                        , param: me.getParams() 
                        , success: function (rsp) { 
                            hcCore.showSuccess('新增成功'); 
                            me.tableData.records.push(rsp.data); 
                            me.tableData.total += 1; 
                            me.edit.visible = false; 
                        } 
                    }); 
                } else { 
                    return false; 
                } 
            }); 
        } 
        , onUpdate: function () { 
            let me = this; 
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => { 
                if (valid) { 
                    hcCore.post({ 
                        url: '/SysParam/Update' 
                        , param: me.getParams() 
                        , success: function (rsp) { 
                            hcCore.showSuccess('更新成功'); 
                            let index = me.tableData.records.findIndex(item => item.param_no == rsp.data.param_no); 
                            me.$set(me.tableData.records, index, rsp.data) 
                            me.edit.visible = false; 
                        } 
                    }); 
                } else { 
                    return false; 
                } 
            }); 
        } 
        , onDelete: function (row) { 
            let me = this; 
            let rows = []; 
            if (row) { 
                rows = [row]; 
            } 
            else { 
                rows = me.$refs.tblRecords.selection; 
            } 
            if (rows && rows.length > 0) { 
                let tips = rows.length == 1 ? ('确认要删除【'+ rows[0].param_no +'】这条记录吗') : ('确认要删除选择【' + rows.length + '】条的记录吗？') 
                hcCore.confirm(tips, function () { 
                    let arr = rows.map(item => { return item.param_no }); 
                    let nos = arr.join(','); 
                    hcCore.post({ 
                        url: '/SysParam/Delete' 
                        , param: { 
                            param_nos: nos 
                        } 
                        , success: function (rsp) { 
                            hcCore.showSuccess('删除成功'); 
                            arr.forEach(item => { 
                                let index = me.tableData.records.findIndex(node => item == node.param_no); 
                                me.tableData.records.splice(index, 1); 
                            }); 
                        } 
                    }); 
                }); 
            } 
            else { 
                hcCore.showError('请选择要删除的记录'); 
            } 
        } 
        , showEditSub: async function (type, title, row) {
            let me = this;
            //console.log(me.editSub.fields);
            //if (me.$refs.editSub.$refs[me.editSub.formId] !== undefined) {
            //    me.$refs.editSub.$refs[me.editSub.formId].resetFields();
            //}
            //me.editSub.fields.forEach((item, index) => {
            //    item.disabled = false;
            //});
            me.resetParams(me.editSub.fields);
            me.editSub.title = title;
            me.editSub.op = type == 'add' ? me.onInsertSub : (type == 'edit' ? me.onUpdateSub : function () { });
            if (type == 'edit') {
                if (row) {
                    await hcCore.post({
                        url: '/SysParamSub/Get/' + row.sub_no
                        , success: function (rsp) {
                            me.editSub.fields.forEach((item, index) => {
                                if (rsp.data[item.name]) {
                                    item.value = rsp.data[item.name];
                                }

                                if (item.name == 'param_no') {
                                    item.disabled = true;
                                }
                            });
                        }
                        , fault: '获得数据失败'
                    });
                }
                else {
                    hcCore.showError('请选择要编辑的记录');
                    return;
                }
            } else {
                let paramNoObj = me.editSub.fields.find(item => item.name == 'param_no');
                paramNoObj.value = me.opObj.paramNo;
            }
            me.editSub.visible = true;
        } 
        , onInsertSub: function () {
            let me = this;
            me.$refs.editSub.$refs[me.editSub.formId].validate((valid) => {
                if (valid) {
                    hcCore.post({
                        url: '/SysParamSub/Insert'
                        , param: me.getSubParams()
                        , success: function (rsp) {
                            hcCore.showSuccess('新增子项成功');
                            me.subParam.records.push(rsp.data);
                            me.editSub.visible = false;
                        }
                    });
                } else {
                    return false;
                }
            }); 
        }
        , onUpdateSub: function () {
            let me = this;
            me.$refs.editSub.$refs[me.editSub.formId].validate((valid) => {
                if (valid) {
                    hcCore.post({
                        url: '/SysParamSub/Update'
                        , param: me.getSubParams()
                        , success: function (rsp) {
                            hcCore.showSuccess('更新子项成功');
                            let index = me.subParam.records.findIndex(item => item.sub_no == rsp.data.sub_no);
                            me.$set(me.subParam.records, index, rsp.data)
                            me.editSub.visible = false;
                        }
                    });
                } else {
                    return false;
                }
            });
        }
        , onDeleteSub: function (row) {
            let me = this;
            let rows = [];
            if (row) {
                rows = [row];
            }
            else {
                rows = me.$refs.tblSubRecords.selection;
            }
            if (rows && rows.length > 0) {
                let tips = rows.length == 1 ? ('确认要删除当前记录吗') : ('确认要删除选择【' + rows.length + '】条的记录吗？')
                hcCore.confirm(tips, function () {
                    let arr = rows.map(item => { return item.sub_no });
                    let nos = arr.join(',');
                    hcCore.post({
                        url: '/SysParamSub/Delete'
                        , param: {
                            sub_nos: nos
                        }
                        , success: function (rsp) {
                            hcCore.showSuccess('删除成功');
                            arr.forEach(item => {
                                let index = me.subParam.records.findIndex(node => item == node.sub_no);
                                me.subParam.records.splice(index, 1);
                            });
                        }
                    });
                });
            }
            else {
                hcCore.showError('请选择要删除的记录');
            } 
        }
        , getParams: function () { 
            let param = {}; 
            this.edit.fields.forEach((item, index) => { 
                param[item.name] = item.value; 
            }); 
            return param; 
        } 
        , getSubParams: function () {
            let param = {};
            this.editSub.fields.forEach((item, index) => {
                param[item.name] = item.value;
            });
            return param;
        } 
        , resetParams: function (obj) {
            obj.forEach(item => {
                item.value = '';
                item.disabled = false;
            });
        }
        , recordIndex: function (index) { 
            return (this.query.page - 1) * this.query.pageSize + index + 1; 
        } 
    } 
});